package com.dragon.modules.yuegou.service.impl.busd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.busd.BtypeEntity;
import com.dragon.modules.yuegou.enums.busd.BtypeType;
import com.dragon.modules.yuegou.enums.code.MerchantCode;
import com.dragon.modules.yuegou.mapper.busd.BtypeMapper;
import com.dragon.modules.yuegou.model.busd.Btype;
import com.dragon.modules.yuegou.query.busd.BtypeQuery;
import com.dragon.modules.yuegou.service.busd.BtypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业态信息表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-07-24 11:00:28
 * @version 2.0
 */
@Service("busdBtypeService")
public class BtypeServiceImpl
		extends ServiceImpl<BtypeMapper, BtypeEntity>
		implements BtypeService {

	@Override
	public List<Btype> secondList(String busdId) {
		return baseMapper.secondList(busdId);
	}

	@Override
	public List<Btype> btypeListChange(String busdId) {
		return baseMapper.btypeListChange(busdId);
	}

	@Override
	public IPage<Btype> search(PageRequest page, BtypeQuery query) {
		Page<BtypeEntity> pa = new Page<>(page.getPage(), page.getSize(), true);
		query.setType(BtypeType.ONE);
		query.setDel(false);

		QueryWrapper<BtypeEntity> qw = query.toQueryWrapper();
		qw.orderByAsc(BtypeEntity.COL_BTYPE_SORT);
		qw.orderByDesc(BtypeEntity.COL_CREATED_TIME);

		IPage<BtypeEntity> iPage = baseMapper.selectPage(pa,qw);
		IPage<Btype> btypeIPage =iPage.convert(Btype::new);
		for (Btype btype : btypeIPage.getRecords()) {
			BtypeQuery  btypeQuery = new BtypeQuery();
			btypeQuery.setParentId(btype.getId());
			btypeQuery.setDel(false);

			QueryWrapper<BtypeEntity> btypeQueryWrapper = query.toQueryWrapper();
			btypeQueryWrapper.orderByAsc(BtypeEntity.COL_BTYPE_SORT);
			btypeQueryWrapper.orderByDesc(BtypeEntity.COL_CREATED_TIME);
			List<BtypeEntity> list = baseMapper.selectList(btypeQueryWrapper);
			List<Btype> btypeList = list.stream().map(Btype::new).collect(Collectors.toList());
			btype.setBtypeList(btypeList);
		}
		return btypeIPage;
	}

	@Override
	public void saveBtype(Btype btype) {
		BtypeQuery btypeQuery = new BtypeQuery();
		btypeQuery.setDel(false);
		btypeQuery.setName(btype.getName());
		List<BtypeEntity> list = baseMapper.selectList(btypeQuery.toQueryWrapper());
		if (StringUtils.isEmpty(btype.getId())) {
			if (list.size() > 0) {
				throw new DragonException(MerchantCode.BTYPE_NAME_EXISTS);
			}
			if (StringUtils.isEmpty(btype.getParentId())) {
				btype.setParentId("0");
				btype.setType(BtypeType.ONE);
			}else {
				btype.setType(BtypeType.TWO);
			}
			btype.setDel(false);
			save(btype);
		}else {
			if (list.size() > 0 && !list.get(0).getId().equals(btype.getId())) {
				throw new DragonException(MerchantCode.BTYPE_NAME_EXISTS);
			}
			baseMapper.updateById(btype);
		}
	}

	@Override
	public void ratioBtype(String btypeId, BigDecimal feeRatio) {
		BtypeEntity btype = baseMapper.selectById(btypeId);
		btype.setFeeRatio(feeRatio);
		baseMapper.updateById(btype);
	}

	@Override
	public void modSort(String id, Integer sort) {
		BtypeEntity btype = baseMapper.selectById(id);
		btype.setBtypeSort(sort);
		baseMapper.updateById(btype);
	}

	@Override
	public List<Btype> searchBTypeList(String parentId) {
		BtypeQuery query = new BtypeQuery();
		query.setParentId(parentId);
		query.setDel(false);
		List<BtypeEntity> list = baseMapper.selectList(query.toQueryWrapper());
		return list.stream().map(Btype::new).collect(Collectors.toList());
	}

	@Override
	public List<Btype> searchGoHomeBtypeInfo(String busdId) {
		return baseMapper.searchGoHomeBtypeInfo(busdId);
	}

	@Override
	public List<Btype> searchBTypeListById(List<String> idList){
		if (CollectionUtils.isEmpty(idList)){
			return new ArrayList<>();
		}
		BtypeQuery query = new BtypeQuery();
		query.id().in(idList);
		query.setDel(false);
		List<BtypeEntity> list = baseMapper.selectList(query.toQueryWrapper());
		return list.stream().map(Btype::new).collect(Collectors.toList());
	}

}